package psl;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: phiteshkumar
 * Date: Feb 27, 2007
 * Time: 4:28:16 PM
 */
public class ItemList extends BasePage
{
    /*
        public String msProductId;
        public String msProductName;
        public String msCategoryName;
        public String msCompany;
        public String msProductPrice;
    */
    public ItemList ()
    {
        msViewPage = "ItemList.jsp";
        msDetailPageId = "ItemBill";
    }

/*
    public void setHttpRequest (HashMap fhRequest)
    {
        super.setRequest (fhRequest);
        msProductId = (String) fhRequest.get ("productId");
        msProductName = (String) fhRequest.get ("productName");
        msProductPrice = (String) fhRequest.get ("productPrice");
        msCategoryName = (String) fhRequest.get ("categoryName");
        msCompany = (String) fhRequest.get ("company");
    }
*/

    public void performProcess (String[] faSelected, String fsTag)
    {
        if (faSelected != null)
        {
            if (moUserSession.mvSelsected != null)
            {
                moUserSession.flag = false;
            }
            if (moUserSession.flag)
            {
                moUserSession.mvSelsected = new ArrayList ();
            }
            if (fsTag.equalsIgnoreCase ("add"))
            {

                for (int i = 0; i < faSelected.length; i++)
                {
                    if (moUserSession.mvSelsected.contains (faSelected[i]))
                    {
                    }
                    else
                    {
                        moUserSession.mvSelsected.add (faSelected[i]);
                        moUserSession.mvQuantity.put(faSelected[i], "1");
                    }
                }
            }
            else if (fsTag.equalsIgnoreCase ("remove"))
            {
                for (int i = 0; i < faSelected.length; i++)
                {
                    moUserSession.mvSelsected.remove (faSelected[i]);
                    moUserSession.mvQuantity.remove (faSelected[i]);
                }
            }
        }
        else
        {
            System.err.println ("no value");
        }

    }

    public void setConstant (HashMap fhRequest)
    {
        String[] laAction = (String[]) fhRequest.get ("action_fld");
        String fsTag = laAction[1];
        if (fsTag.equals ("next"))
        {
            String  row_no=(String) fhRequest.get ("row_0");
            if(row_no!=null)
            {
              if(!row_no.equals (""))
              {
                  if(!miPreRow.contains (Integer.parseInt (row_no)))
                  miPreRow.add (Integer.parseInt (row_no));
              }
            }
        }
        else if (fsTag.equals ("start"))
        {
            miStartRow = 0;
        }
        else if (fsTag.equals ("end"))
        {
        }
        else if (fsTag.equals ("previous"))
        {
            miStartRow = miPreRow.size()<1?0:(Integer)miPreRow.get (miPreRow.size ()-1);
            if(miPreRow.size ()>0)
            {
                miPreRow.remove (miPreRow.size ()-1);
            }
        }
        else
        {
            miStartRow = 0;
            moUserSession.msPageID = fsTag;
        }
    }

    public void setTempData (String fsUser, ArrayList fsPid)
    {
        if (fsUser != null)
        {
            if (!fsUser.equalsIgnoreCase (""))
            {
                if (fsPid != null)
                {
                    if (fsPid.size () != 0)
                    {
                        try
                        {
                            String lsCurDate = (AppConstants.SIMPLE_DATE_FORMAT).format (new java.util.Date ());
                            Iterator lItr = fsPid.iterator ();
                            String laproductDetail[] = { "cat_name", "product_name", "product_price", "company" };
                            String lsOrderNo = CommonServices.getMaxNoFormDB ("tempdata", "order_no", "cid='" + moUserSession.msCustId + "'", "O");
                            while (lItr.hasNext ())
                            {
                                String[] lsQuantity = null;
                                String lsPid = (String) lItr.next ();
                                String lsProDetQuery = "select  cat_name, product_name, product_price, company from product_detail_web where " +
                                        "product_id='" + lsPid + "'";
                                String[][] laProDetail = DBConn.setRecordsToString (lsProDetQuery, laproductDetail, "");
                                float lfprice = Float.parseFloat (laProDetail[2][0]);
                                String lsQuery = "Insert into tempdata" +
                                        " values (" +
                                        "'" + fsUser + "'," +
                                        "'" + laProDetail[0][0] + "'," +
                                        "'" + laProDetail[1][0] + "'," +
                                        "" + lfprice + "," +
                                        "'" + laProDetail[3][0] + "'," +
                                        1 + "," +
                                        "" + lfprice + "," +
                                        "'" + AppConstants.STATUS_NOT_DELEVERED + "'," +
/*
                                        "TO_DATE('" + lsCurDate + "','" + AppConstants.SQL_DATE_FORMATE + "'), " +
*/
                                        "'" + lsCurDate + "'," +
                                        "'" + lsPid + "'," +
                                        "'" + lsOrderNo + "'" +
                                        ")";
                                moUserSession.msError = DBConn.executeUpdate (lsQuery);
                                if (moUserSession.msError != null)
                                {
                                    moUserSession.msError += lsQuery;
                                    return;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace ();
                        }
                    }
                }
            }
        }
    }
}
